package com.stone.business.workcard.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.workcard.pojo.Gxzlkzk;
import com.stone.business.workcard.pojo.Gxzlkzk_GX;
import com.stone.business.workcard.service.WorkcardService;
import com.stone.sys.pojo.TBzgsk;
import com.stone.sys.pojo.TMenu;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.CustomPage;

public class GxzlkzkListAction extends BaseListAction {
    private String start;
	private String limit;
	// 选中工序的Id
	private String id;
	private WorkcardService workcardService;
	@Override
	public HashMap<String, Object> setSearchCondition() {
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();
		
		m.put("cols", " a ");
		
		String conditions = " 1=1 "; 
		
		/*if (!"1".equals(this.getSessionAttribute(IConstant.SESSION_ISADMIN))) {
			conditions += " AND a.ouid = '" + super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE) + "'";
		}*/
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(a.dt_czrq,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(a.dt_czrq,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		/*if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND userid like ?";
			valsList.add("%" + super.getSearchBean().getCon3() + "%");
		}*/
		
		// 查询条件
		m.put("conditions",conditions);
		m.put("clause", " order by a.id_sno Desc");
		m.put("alias", " a ");
		m.put("vals", valsList.toArray());
		
		return m;
	}
	/**
	 * 用户给工序时的Ext.Grid查询
	 * 
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public void getListExt() throws Exception {

		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		
		HashMap<String, Object> m = new HashMap<String, Object>();
	
		m.put("cols", "btn.id as id, btn.oc_gx as ocGx, btn.oc_gybzjyq as ocGybzjyq, btn.oc_fxtx as ocFxtx, btn.sortId as sortId");
		m.put("alias", "btn");
		m.put("join", "");

		m.put("conditions"," btn.parentId = " + id);
		m.put("clause", " order by btn.sortId");

		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = service.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()), m, Gxzlkzk_GX.class.getName());
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);
 
        super.getResponse().getWriter().print("{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql((String) m.get("conditions"), null, Gxzlkzk_GX.class.getName()+ " as btn") + "]}");
		
	}
	public void getModelList() throws IOException, AppException {
        super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		HashMap<String, Object> m = new HashMap<String, Object>();
		String conditions = " a.model = '1'";
		if(!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())&&!super.getSearchBean().getCon1().equals("0")) {
			conditions += " and a.oc_gzbz = '" + super.getSearchBean().getCon1() + "'";
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND a.dydj = '" + super.getSearchBean().getCon2() + "'";
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND a.sblx = '" + super.getSearchBean().getCon3() + "'";
		}
		
		m.put("cols", " a.id_sno as id,a.oc_gzbz as gzbz, a.oc_zymc as zymc,a.oc_gzph as gzph");
		m.put("alias", " a ");
		m.put("conditions",conditions);
		m.put("clause", " order by a.id_sno");
		
		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = service.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()) , m, Gxzlkzk.class.getName() );
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);
        String rtn = "{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql(conditions, null, Gxzlkzk.class.getName() + m.get("alias")  ) + "]}";
		
		//String strjson = "{result:" + json.toString() + ",totalCount:[" + list.size() + "]}";
		
        super.getResponse().getWriter().print(rtn);
		
	}
	public WorkcardService getWorkcardService() {
		return workcardService;
	}
	public void setWorkcardService(WorkcardService workcardService) {
		this.workcardService = workcardService;
		this.service=workcardService;
	}
	public GxzlkzkListAction() {
		this.entityClass = Gxzlkzk.class;
	}
	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		//return limit == null ? String.valueOf(CustomPage.PER_PAGE) : limit;
		return limit == null ? String.valueOf(30) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}
	public void setId(String id) {
		this.id = id;
	}
}
